<template>
  <div id="container">
    <el-row :gutter="40">
      <!-- 报修通道 -->
      <el-col :xs="{span:24,offset:0}" :sm="{span:9,offset:0}">
        <div class="left">
          <div class="baoxiu">
            <p>
              <span class="el-icon-s-tools"></span>报修通道
            </p>
            <el-dropdown trigger="click" placement="top-end" @command="goRouter">
              <el-button type="primary">
                我要报修
                <i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="table">报修查询</el-dropdown-item>
                <el-dropdown-item command="home/form">立即报修</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>

          <div class="phone">
            <p>
              <span class="el-icon-phone"></span>服务电话
            </p>
            <ul>
              <li>维修中心电话：12345678910</li>
              <li>维修中心电话：12345678910</li>
              <li>维修中心电话：12345678910</li>
              <li>维修中心电话：12345678910</li>
            </ul>
          </div>
        </div>
      </el-col>
      <!-- 公告通知 -->
      <el-col :xs="{span:24,offset:0}" :sm="{span:15,offset:0}">
        <div class="right">
          <p>
            <span class="el-icon-date"></span>最新公告
            <router-link to="/noticelist">更多&gt;&gt;</router-link>
          </p>
          <div class="info">
            <div class="biaoti">
              <h3>标题</h3>
              <h3>日期</h3>
            </div>
            <hr size="2px" color="#0075a9" />
            <div class="news" v-for="item in noticeData" :key="item.id">
              <router-link
                :to="'/noticelist/noticeinfo/'+item.id"
                class="title"
                tag="p"
              >{{item.title}}</router-link>
              <p class="date">{{item.time | dateFormat}}</p>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>

    <!-- 报修列表 -->
    <el-row>
      <el-col :span="24" :offset="0" :xs="{span:24,offset:0}">
        <div class="bx-list">
          <p>
            <span class="el-icon-s-custom"></span>日常报修
            <router-link to="/table">更多&gt;&gt;</router-link>
          </p>
          <!-- 列表组件 -->
          <el-table
            :data="tableData"
            stripe
            style="width: 100%"
            :header-cell-style="headClass"
            :cell-style="tableClass"
          >
            <el-table-column fixed prop="id" label="编号" width="80"></el-table-column>
            <el-table-column prop="stuId" label="学号" min-width="120"></el-table-column>
            <el-table-column prop="descrip" label="报修原因" min-width="150"></el-table-column>
            <el-table-column prop="place" label="地点" min-width="100"></el-table-column>
            <el-table-column
              :formatter="dateFormat"
              prop="time"
              label="报修时间"
              width="140"
              align="center"
            ></el-table-column>
            <el-table-column prop="status" fixed="right" label="报修状态" width="100"></el-table-column>
          </el-table>
        </div>
      </el-col>
    </el-row>

    <!-- echart统计图 -->
    <el-row>
      <el-col :span="24" :offset="0" :xs="{span:24,offset:0}" @open="open()">
        <div class="echart">
          <p>
            <span class="el-icon-s-data"></span>维修统计
          </p>
          <h2>2019-2020学年数据统计</h2>
        </div>
        <div ref="chartmain" style="width:100%;height:400px;"></div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      noticeData: [], //公告列表
      tableData: [] // 首页列表数据
    };
  },
  mounted() {
    this.getNoticeList();
    this.getHomelist();
    this.drawEchart();
  },
  methods: {
    //解决在elementui布局中使用echarts统计图不显示问题，elementui中dialog组件是懒渲染的，要在open事件里面初始化echarts
    open() {
      const t = this;
      setTimeout(() => {
        //  执行echarts画图方法
        t.drawLine();
      }, 0);
    },
    //echart统计图
    drawEchart() {
      var option = {
        title: {
          text: "",
          subtext: ""
        },
        tooltip: {
          trigger: "axis"
        },
        legend: {
          data: ["物业管理中心", "生活服务中心"]
        },
        toolbox: {
          show: true,
          feature: {
            magicType: { show: false, type: ["line", "bar"] },
            restore: { show: false },
            saveAsImage: { show: false }
          }
        },
        calculable: true,
        xAxis: [
          {
            type: "category",
            data: [
              "1月",
              "2月",
              "3月",
              "4月",
              "5月",
              "6月",
              "7月",
              "8月",
              "9月",
              "10月",
              "11月",
              "12月"
            ]
          }
        ],
        yAxis: [
          {
            type: "value"
          }
        ],
        series: [
          {
            name: "物业管理中心",
            type: "bar",
            data: [2, 4, 7, 23, 25, 76, 35, 62, 32, 20, 6, 3],
            markPoint: {
              data: [
                { type: "max", name: "最大值" },
                { type: "min", name: "最小值" }
              ]
            },
            markLine: {
              data: [{ type: "average", name: "平均值" }]
            }
          },
          {
            name: "生活服务中心",
            type: "bar",
            data: [2, 5, 9, 26, 28, 70, 75, 82, 48, 18, 6, 2],
            // markPoint: {
            //   data: [
            //     { name: "年最高", value: 82, xAxis: 7, yAxis: 83 },
            //     { name: "年最低", value: 2, xAxis: 11, yAxis: 3 }
            //   ]
            // },
            markPoint: {
              data: [
                { type: "max", name: "最大值" },
                { type: "min", name: "最小值" }
              ]
            },
            markLine: {
              data: [{ type: "average", name: "平均值" }]
            }
          }
        ]
      };
      var myChart = this.$echarts.init(this.$refs.chartmain);
      myChart.setOption(option);
      // 窗口大小改变立马重新渲染自适应
      window.addEventListener("resize", function() {
        myChart.resize();
      });
    },
    //路由跳转formbx表单
    goRouter(comments) {
      this.$router.push({ path: comments });
    },
    //获取公告列表
    getNoticeList() {
      this.$axios
        .get("/api/article/queryNewArticleList")
        .then(res => {
          this.noticeData = res.data.splice(0,6);
        })
        .catch(err => {
          this.$message.error("获取列表失败");
        });
    },
    // 获取报修列表信息
    getHomelist() {
      this.$axios
        .get("/api/order/queryListByPage/1/8")
        .then(res => {
          res.data.orders.forEach(item => {
              if (item.status == 0) {
                item.status = "已关闭";
              } else if (item.status == 1) {
                item.status = "未处理";
              } else if (item.status == 2) {
                item.status = "处理中";
              } else if (item.status == 3) {
                item.status = "暂缓维修";
              } else if (item.status == 4) {
                item.status = "已完成";
              }
            });
          this.tableData = res.data.orders;
        })
        .catch(err => {
          this.$message.error("获取列表失败");
        });
    },
    //列表日期格式
    dateFormat(row, column, cellValue) {
      var date = new Date(cellValue); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var Y = date.getFullYear() + "-";
      var M =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var D = date.getDate() + " ";
      return Y + M + D;
    },
    // 表头样式设置
    headClass() {
      return "text-align: center;";
    },
    // 单元格样式
    tableClass() {
      return "text-align: center;";
    }
  }
};
</script>

<style lang="scss" scoped>
#container {
  margin: 0 auto;
  max-width: 1200px;
}
p {
  span {
    display: inline-block;
    vertical-align: middle;
    width: 28px;
    height: 28px;
    font-size: 24px;
    margin-left: 8px;
    margin-top: -2px;
  }
}
.el-popper[x-placement^="top"] {
  margin-bottom: -10px;
  margin-left: 50px;
}
.el-popper[x-placement^="bottom"] {
  margin-top: -10px;
  margin-left: 50px;
}
.el-icon-arrow-down {
  font-size: 14px;
}
.left {
  margin-top: 10px;
  margin-bottom: 30px;
  .baoxiu {
    text-align: center;
    p {
      text-align: left;
      background-color: #eee;
      font-size: 20px;
      line-height: 40px;
      height: 40px;
      color: #0075a9;
    }
    button {
      width: 200px;
      height: 50px;
      background-color: #0075a9;
      margin: 10px;
      font-size: 20px;
    }
  }
  .phone {
    background-color: #eee;
    font-size: 20px;
    line-height: 40px;
    height: 40px;
    p {
      color: #0075a9;
    }
    ul {
      column-count: 2;
      -moz-column-count: 2;
      -webkit-column-count: 2;
      li {
        font-size: 14px;
        list-style: none;
        padding-left: 10px;
      }
    }
    @media screen and (max-width: 768px) {
      ul {
        float: left;
        column-count: 1;
        -moz-column-count: 1;
        -webkit-column-count: 1;
        margin-left: 20px;
      }
    }
  }
}
.right {
  margin-top: 10px;
  margin-bottom: 30px;
  p {
    background-color: #eee;
    font-size: 20px;
    line-height: 40px;
    height: 40px;
    color: #0075a9;
    a {
      float: right;
      margin-right: 20px;
      font-size: 14px;
    }
    a:hover {
      text-decoration: underline;
    }
  }
  .info {
    min-height: 220px;
    .biaoti {
      display: flex;
      justify-content: space-between;
      h3 {
        margin: 6px 50px 6px 50px;
      }
    }
  }
  .news {
    display: flex;
    justify-content: space-between;
    .title {
      text-overflow: ellipsis;
    }
    p {
      background-color: #fff;
      color: #383838;
      font-size: 16px;
      padding: 0 20px;
      height: 30px;
      cursor: pointer;
      width: 80%;
      margin-right: 10px;
      text-overflow: ellipsis;
    }

    .date {
      width: 100px;
      font-size: 12px;
      padding: 0;
      margin-right: 20px;
      text-align: center;
    }
    p:hover {
      color: #0075a9;
    }
  }
}
.bx-list {
  margin-bottom: 30px;
  p {
    background-color: #eee;
    font-size: 20px;
    line-height: 40px;
    height: 40px;
    color: #0075a9;
    a {
      float: right;
      margin-right: 20px;
      font-size: 14px;
    }
    a:hover {
      text-decoration: underline;
    }
  }
}
.echart {
  p {
    text-align: left;
    background-color: #eee;
    font-size: 20px;
    line-height: 40px;
    height: 40px;
    color: #0075a9;
  }
  h2 {
    width: 100%;
    margin: 16px auto;
    text-align: center;
  }
}
</style>